using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using Unity.VisualScripting;
using UnityEngine;
using static GiftManager;

public class GiftId{
    public const string XianNvBang = "n1/Dg1905sj1FyoBlQBvmbaDZFBNaKuKZH6zxHkv8Lg5x2cRfrKUTb8gzMs=";
    public const string NengLiYaoWan = "28rYzVFNyXEXFC8HI+f/WG+I7a6lfl3OyZZjUS+CVuwCgYZrPrUdytGHu0c=";
    public const string MoFaJing = "fJs8HKQ0xlPRixn8JAUiL2gFRiLD9S6IFCFdvZODSnhyo9YN8q7xUuVVyZI=";
    public const string TianTianQuan = "PJ0FFeaDzXUreuUBZH6Hs+b56Jh0tQjrq0bIrrlZmv13GSAL9Q1hf59fjGk=";
    public const string AiDeBaoZha = "gx7pmjQfhBaDOG2XkWI2peZ66YFWkCWRjZXpTqb23O/epru+sxWyTV/3Ufs=";
    public const string ShenMiKongTou = "pGLo7HKNk1i4djkicmJXf6iWEyd+pfPBjbsHmd3WcX0Ierm2UdnRR7UINvI=";

    public const string NengLiangDianChi = "IkkadLfz7O/a5UR45p/OOCCG6ewAWVbsuzR/Z+v1v76CBU+mTG/wPjqdpfg=";
}

public class GiftSocket : MonoBehaviour
{
    private ReceiveSocket receiveSocket;
    private StudentManager studentManager;
    private GiftManager giftManager;

    void Start()
    {
        // 获取ReceiveSocket实例
        receiveSocket = FindObjectOfType<ReceiveSocket>();
        studentManager = FindObjectOfType<StudentManager>();
        giftManager = FindObjectOfType<GiftManager>();
        if (receiveSocket == null)
        {
            Debug.LogError("未找到ReceiveSocket实例");
            return;
        }
        if (studentManager == null)
        {
            Debug.LogError("未找到StudentManager实例");
            return;
        }
        if (giftManager == null){
            Debug.LogError("未找到GiftManager实例");
            return;
        }

        // 订阅礼物接收事件
        receiveSocket.OnLiveGiftReceived += HandleGift;
    }

    void OnDestroy()
    {
        // 取消订阅
        if (receiveSocket != null)
        {
            receiveSocket.OnLiveGiftReceived -= HandleGift;
        }
    }

    private void HandleGift(LiveGiftData gift)
    {
        // 先检查用户信息是否存在
        if (gift.game_user == null)
        {
            Debug.LogWarning($"用户{gift.nickname}没有game_user信息");
            return;
        }

        // 获取用户等级
        int userLevel = gift.game_user.level;
        int userId = gift.game_user.id;
        
        Debug.Log($"收到礼物: {gift.nickname} 赠送了 {gift.gift_num} 个礼物，价值 {gift.gift_value}");

        // 如果用户不存在，则忽略礼物
        if (!studentManager.ContainsStudent(userId.ToString()))
        {

            //不忽略礼物，改为让用户进场
            studentManager.UserEnter(new LiveCommentData{
                game_user = gift.game_user,
                avatar_url = gift.avatar_url,
                nickname = gift.nickname
            });
            Debug.Log($"用户{gift.nickname}不存在，改为让用户进场");
        }
        GameObject student = studentManager.GetStudentById(userId.ToString());

        //先看是什么类型的玩法
        if(PlayerPrefs.GetString(GameMode.Mode) == GameMode.Single){
            //根据不同的礼物id，处理不同的礼物
            AnimaName gift_animation_index = AnimaName.DianZan;
            switch (gift.sec_gift_id){
                case GiftId.XianNvBang: // 1
                    //额外判断是不是66个仙女棒
                    if (gift.gift_num == 66){
                        StringEventSystem.Global.Send(EventID.PlayGiftAnimation, new object[]{AnimaName.YanHua,student});
                        return;
                    }else{
                        gift_animation_index = AnimaName.StudentSpeak;
                    }
                    break;
                case GiftId.NengLiYaoWan: 
                    gift_animation_index = AnimaName.YouCome;
                    break;
                case GiftId.TianTianQuan:
                    gift_animation_index = AnimaName.TeacherSpeak;
                    break;
                case GiftId.AiDeBaoZha:
                    gift_animation_index = AnimaName.ChiTiZhengQue;
                    StringEventSystem.Global.Send(EventID.GiftPersonalRight, student);
                    break;
                case GiftId.ShenMiKongTou:
                    gift_animation_index = AnimaName.TiaoZhanNo1;
                    StringEventSystem.Global.Send(EventID.GiftSingleTiaoZhanNo1,student);
                    break;
                case GiftId.NengLiangDianChi: 
                    gift_animation_index = AnimaName.JiFenFanBei;
                    student.GetComponentInChildren<Student>().setBeishu();
                    break;
                default: 
                    gift_animation_index = AnimaName.DianZan;
                    break;
            }
            for (int i = 0; i < gift.gift_num; i++)
            {
                StringEventSystem.Global.Send(EventID.PlayGiftAnimation, new object[]{gift_animation_index,student});
                StringEventSystem.Global.Send(EventID.GiftJifen, gift.gift_value);
                Debug.Log("礼物数量："+gift.gift_num+"  礼物价值："+gift.gift_value);
            }
        }else{
            //班级赛
            //根据不同的礼物id，处理不同的礼物
            AnimaName gift_animation_index = AnimaName.DianZan;
            switch (gift.sec_gift_id){
                case GiftId.XianNvBang: // 1
                    gift_animation_index = AnimaName.YanHua;
                    break;
                case GiftId.NengLiYaoWan: 
                    gift_animation_index = AnimaName.QingZhuLiHua;
                    break;
                case GiftId.MoFaJing:
                    gift_animation_index = AnimaName.YouCome;
                    break;
                case GiftId.TianTianQuan:
                    gift_animation_index = AnimaName.JianShaoCuoWu;
                     StringEventSystem.Global.Send(EventID.GiftClassReduceWrong,student);
                    break;
                case GiftId.NengLiangDianChi:
                    gift_animation_index = AnimaName.QuanBanZhengQue;
                    StringEventSystem.Global.Send(EventID.GiftClassAllRight,student);
                    break;
                case GiftId.AiDeBaoZha:
                    gift_animation_index = AnimaName.JiFenFanBei;
                    StringEventSystem.Global.Send(EventID.GiftClassJiFenFanBei,student);
                    break;
                case GiftId.ShenMiKongTou: 
                    gift_animation_index = AnimaName.JueDiFanJi;
                     StringEventSystem.Global.Send(EventID.GiftClassJueDiFanJi,student);
                    break;
                default: 
                    gift_animation_index = AnimaName.DianZan;
                    break;
            }
            for (int i = 0; i < gift.gift_num; i++)
            {
                StringEventSystem.Global.Send(EventID.PlayGiftAnimation, new object[]{gift_animation_index,student});
                StringEventSystem.Global.Send(EventID.GiftJifen, gift.gift_value);
                Debug.Log("礼物数量："+gift.gift_num+"  礼物价值："+gift.gift_value);
            }
        }
    }
}